<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>JDBC | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="JDBC JDBC 的概述1.1 什么是 JDBCJDBC(Java Data Base Connectivity.Java数据库连接), 是一种用于执行 SQL 语句的 Java API, 可以为多种关系型数据库提供统一访问, 它由一组用 Java 语言编写的类和接口组成. 1.2 什么是数据库驱动 驱动: 两个设备(应用)之间通信的桥梁.  1.3 为什么学习 JDBC没有 JDBC 的时候,">
<meta property="og:type" content="article">
<meta property="og:title" content="JDBC">
<meta property="og:url" content="https://lummer.gitee.io/hexo/2021/01/26/JDBC/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="JDBC JDBC 的概述1.1 什么是 JDBCJDBC(Java Data Base Connectivity.Java数据库连接), 是一种用于执行 SQL 语句的 Java API, 可以为多种关系型数据库提供统一访问, 它由一组用 Java 语言编写的类和接口组成. 1.2 什么是数据库驱动 驱动: 两个设备(应用)之间通信的桥梁.  1.3 为什么学习 JDBC没有 JDBC 的时候,">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Lummer-Li/image-bucket/image/20210126221815.png">
<meta property="article:published_time" content="2021-01-26T12:35:54.272Z">
<meta property="article:modified_time" content="2021-09-20T10:10:52.609Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/Lummer-Li/image-bucket/image/20210126221815.png">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/hexo/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://lummer.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-JDBC" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/hexo/2021/01/26/JDBC/" class="article-date">
  <time datetime="2021-01-26T12:35:54.272Z" itemprop="datePublished">2021-01-26</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JDBC
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="JDBC"><a href="#JDBC" class="headerlink" title="JDBC"></a>JDBC</h3><hr>
<h4 id="JDBC-的概述"><a href="#JDBC-的概述" class="headerlink" title="JDBC 的概述"></a>JDBC 的概述</h4><h5 id="1-1-什么是-JDBC"><a href="#1-1-什么是-JDBC" class="headerlink" title="1.1 什么是 JDBC"></a>1.1 什么是 JDBC</h5><p>JDBC(Java Data Base Connectivity.Java数据库连接), 是一种用于执行 SQL 语句的 Java API, 可以为多种关系型数据库提供统一访问, 它由一组用 Java 语言编写的类和接口组成.</p>
<h5 id="1-2-什么是数据库驱动"><a href="#1-2-什么是数据库驱动" class="headerlink" title="1.2 什么是数据库驱动"></a>1.2 什么是数据库驱动</h5><ul>
<li>驱动: 两个设备(应用)之间通信的桥梁.</li>
</ul>
<h5 id="1-3-为什么学习-JDBC"><a href="#1-3-为什么学习-JDBC" class="headerlink" title="1.3 为什么学习 JDBC"></a>1.3 为什么学习 JDBC</h5><p>没有 JDBC 的时候, 如果现在要开发一套系统, 使用 Java 连接 MySQL 数据库, 那么这时候 Java 程序员需要了解 MySQL 驱动 API, 如果使用 Java 连接 Oracle 数据库, 那么这个时候 Java 程序员需要了解 Oracle 数据库驱动 API.</p>
<p>SUN 公司提供了一套统一的规范(接口), 然后各个数据库生产商提供这套接口的实现. 这套接口规范就是 JDBC 的规范.</p>
<h4 id="JDBC-的入门"><a href="#JDBC-的入门" class="headerlink" title="JDBC 的入门"></a>JDBC 的入门</h4><h5 id="2-1-JDBC-的环境准备"><a href="#2-1-JDBC-的环境准备" class="headerlink" title="2.1 JDBC 的环境准备"></a>2.1 JDBC 的环境准备</h5><ol>
<li><p>创建数据库和表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> database web_test3;</span><br><span class="line">use web_test3;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">user</span>(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	username <span class="type">varchar</span>(<span class="number">20</span>),</span><br><span class="line">	password <span class="type">varchar</span>(<span class="number">20</span>),</span><br><span class="line">	nickname <span class="type">varchar</span>(<span class="number">20</span>),</span><br><span class="line">	age <span class="type">int</span></span><br><span class="line">);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> <span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">&#x27;aaa&#x27;</span>, <span class="string">&#x27;123&#x27;</span>, <span class="string">&#x27;小丽	&#x27;</span>, <span class="number">34</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> <span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">&#x27;bbb&#x27;</span>, <span class="string">&#x27;123&#x27;</span>, <span class="string">&#x27;小王	&#x27;</span>, <span class="number">32</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> <span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">&#x27;ccc&#x27;</span>, <span class="string">&#x27;123&#x27;</span>, <span class="string">&#x27;大黄	&#x27;</span>, <span class="number">29</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> <span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">&#x27;ddd&#x27;</span>, <span class="string">&#x27;123&#x27;</span>, <span class="string">&#x27;小明	&#x27;</span>, <span class="number">31</span>);</span><br></pre></td></tr></table></figure></li>
<li><p>创建项目, 引入 jar 包</p>
<p><img src="https://cdn.jsdelivr.net/gh/Lummer-Li/image-bucket/image/20210126221815.png" alt="注意 Build Path"></p>
</li>
<li><p>JDBC 的代码实现</p>
<ul>
<li><p>JDBC 的开发步骤</p>
<ul>
<li>第一步: 加载驱动</li>
<li>第二步: 获得连接</li>
<li>第三步: 基本操作</li>
<li>第四步: 释放资源</li>
</ul>
</li>
<li><p>JDBC 的代码实现</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo1;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.awt.datatransfer.StringSelection;</span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.mysql.cj.protocol.Resultset;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo1</span> </span>&#123;</span><br><span class="line">	<span class="comment">//	JDBC 的入门程序</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">		<span class="comment">// 1. 加载驱动</span></span><br><span class="line">		Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">		<span class="comment">// 2. 获得连接</span></span><br><span class="line">		Connection connection =  DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://localhost:3306/ceshi&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;2caxcazdgn&quot;</span>);</span><br><span class="line">		<span class="comment">// 3. 基本操作: 执行 SQL</span></span><br><span class="line">			<span class="comment">// 3.1 获得执行 SQL 语句的对象</span></span><br><span class="line">		Statement statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 3.2 编写 SQL 语句</span></span><br><span class="line">		String sql = <span class="string">&quot;select * from blog&quot;</span>;</span><br><span class="line">			<span class="comment">// 3.3 执行 SQL 语句</span></span><br><span class="line">		ResultSet rs = statement.executeQuery(sql);</span><br><span class="line">			<span class="comment">// 3.4 遍历结果集合</span></span><br><span class="line">		System.out.println(<span class="string">&quot;--------------------------------&quot;</span>);</span><br><span class="line">		<span class="keyword">while</span> (rs.next()) &#123;</span><br><span class="line">			System.out.print(rs.getInt(<span class="string">&quot;id&quot;</span>) + <span class="string">&quot; &quot;</span>);</span><br><span class="line">			System.out.println(rs.getString(<span class="string">&quot;phone_number&quot;</span>));</span><br><span class="line">			System.out.println(<span class="string">&quot;--------------------------------&quot;</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 4. 释放资源</span></span><br><span class="line">		rs.close();</span><br><span class="line">		statement.close();</span><br><span class="line">		connection.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ol>
<h4 id="JDBC-的-API-详解之-DriverManager"><a href="#JDBC-的-API-详解之-DriverManager" class="headerlink" title="JDBC 的 API 详解之 DriverManager"></a>JDBC 的 API 详解之 DriverManager</h4><h5 id="3-1-DriverManager-驱动管理类"><a href="#3-1-DriverManager-驱动管理类" class="headerlink" title="3.1 DriverManager: 驱动管理类"></a>3.1 DriverManager: 驱动管理类</h5><ol>
<li><p>作用一: 注册驱动</p>
<p>如果需要注册驱动, 就会使用 Drivermanager.registerDriver(new Driver()); 但是查看源代码发现, 在代码中有一段静态代码块, 静态代码块已经调用了注册驱动的方法.</p>
<ul>
<li><p>如果在手动调用该方法注册驱动, 就会导致该驱动注册两次. 实际开发中一般会采用:     </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>作用二: 获得连接</p>
<p>这个方法就是用来获得与数据库连接的方法, 这个方法有三个参数:</p>
<ul>
<li>url: 与数据库连接的路径</li>
<li>user: 与数据库连接的用户名</li>
<li>password: 与数据库连接的密码</li>
<li>主要关注的是 url 的写法: <code>jdbc:mysql://localhost:3306/ceshi</code><ul>
<li>Jdbc: 连接数据库的协议</li>
<li>mysql: 是 jdbc 的子协议</li>
<li>localhost: 连接的 MySQL 数据库服务器的主机地址.(连接是本机就可以写成 localhost), 如果连接的不是本机, 就需要写上连接主机的 IP 地址</li>
<li>3306: MySQL 数据库服务器的端口号</li>
<li>ceshi: 数据库名称</li>
<li>注意: 如果连接的是本机的路径, 可以简化为如下格式: <code>jdbc:mysql:///ceshi</code></li>
</ul>
</li>
</ul>
</li>
</ol>
<h4 id="JDBC-的-API-详解之-Connection"><a href="#JDBC-的-API-详解之-Connection" class="headerlink" title="JDBC 的 API 详解之 Connection"></a>JDBC 的 API 详解之 Connection</h4><h5 id="4-1-Connection-与数据库连接对象"><a href="#4-1-Connection-与数据库连接对象" class="headerlink" title="4.1 Connection: 与数据库连接对象"></a>4.1 Connection: 与数据库连接对象</h5><ol>
<li><p>作用一: 创建执行 SQL 语句的对象</p>
<p>执行 SQL 语句对象</p>
<ul>
<li>Statement: 执行 SQL</li>
<li>CallableStatement: 执行数据库中存储过程</li>
<li>PreparedStatement: 执行 SQL, 对 SQL 进行预处理. 解决 SQL 注入漏洞</li>
</ul>
</li>
<li><p>作用二: 管理事务</p>
</li>
</ol>
<h4 id="JDBC-的-API-详解之-Statement"><a href="#JDBC-的-API-详解之-Statement" class="headerlink" title="JDBC 的 API 详解之 Statement"></a>JDBC 的 API 详解之 Statement</h4><h5 id="5-1-Statement-执行-SQL"><a href="#5-1-Statement-执行-SQL" class="headerlink" title="5.1 Statement: 执行 SQL"></a>5.1 Statement: 执行 SQL</h5><ol>
<li><p>作用一: 执行 SQL</p>
<p>执行 SQL 的方法:</p>
<ul>
<li>boolean execute(String sql);<ul>
<li>执行查询、修改、添加、删除的 SQL 语句</li>
</ul>
</li>
<li><strong>ResultSet executeQuery(String sql);</strong><ul>
<li><strong>执行查询(执行 select 语句)</strong></li>
</ul>
</li>
<li><strong>int executeUpdate(String sql);</strong><ul>
<li><strong>智慧型修改、添加、删除的 SQL 语句</strong></li>
</ul>
</li>
</ul>
</li>
<li><p>作用二: 执行批处理</p>
</li>
</ol>
<h4 id="JDBC-的-API-详解之-ResultSet"><a href="#JDBC-的-API-详解之-ResultSet" class="headerlink" title="JDBC 的 API 详解之 ResultSet"></a>JDBC 的 API 详解之 ResultSet</h4><h5 id="6-1-ResultSet-结果集-通过-select-查询的结果"><a href="#6-1-ResultSet-结果集-通过-select-查询的结果" class="headerlink" title="6.1 ResultSet: 结果集(通过 select 查询的结果)"></a>6.1 ResultSet: 结果集(通过 select 查询的结果)</h5><ol>
<li>结果集的遍历: rs.next()<ul>
<li>结果集的原理</li>
<li>代码实现</li>
</ul>
</li>
<li>结果集的获取<ul>
<li>结果集获取可以使用结果集中的: getXXX(): 方法通常都会有一个重载的方法<ul>
<li>getXXX(int columnIndex);</li>
<li>getXXX(String  columnName);</li>
</ul>
</li>
</ul>
</li>
</ol>
<h4 id="JDBC-的资源释放"><a href="#JDBC-的资源释放" class="headerlink" title="JDBC 的资源释放"></a>JDBC 的资源释放</h4><h5 id="7-1-JDBC-资源释放"><a href="#7-1-JDBC-资源释放" class="headerlink" title="7.1 JDBC 资源释放"></a>7.1 JDBC 资源释放</h5><p>JDBC 程序执行结束后, 将与数据库进行交互的对象释放掉, 通常是 ResultSet、Statement、Connection</p>
<p>这几个对象中尤其是 Connection 对象是非常稀有的, 这个对象一定要做到尽量晚创建, 尽早释放掉.</p>
<ul>
<li><p>将资源释放的代码写入到 finally 的代码块中</p>
</li>
<li><p>资源释放的代码应该写的标准</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(rs != <span class="keyword">null</span>)&#123;</span><br><span class="line">	<span class="keyword">try</span>&#123;</span><br><span class="line">		rs.close();</span><br><span class="line">	&#125;<span class="keyword">catch</span>(SQLException sqlEx)&#123;</span><br><span class="line">		rs = <span class="keyword">null</span>;</span><br><span class="line">		sqlEX.printStackTrace();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span>(stmt != <span class="keyword">null</span>)&#123;</span><br><span class="line">	<span class="keyword">try</span>&#123;</span><br><span class="line">		stmt.close();</span><br><span class="line">	&#125;<span class="keyword">catch</span>(SQLException sqlEx)&#123;</span><br><span class="line">		stmt = <span class="keyword">null</span>;</span><br><span class="line">		sqlEX.printStackTrace();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="JDBC-的-CRUD-操作之保存操作"><a href="#JDBC-的-CRUD-操作之保存操作" class="headerlink" title="JDBC 的 CRUD 操作之保存操作"></a>JDBC 的 CRUD 操作之保存操作</h4><h5 id="8-1-保存操作代码实现"><a href="#8-1-保存操作代码实现" class="headerlink" title="8.1 保存操作代码实现"></a>8.1 保存操作代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 注册驱动</span></span><br><span class="line">			Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://localhost:3306/ceshi&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;2caxcazdgn&quot;</span>);</span><br><span class="line">			<span class="comment">// 执行操作</span></span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句对象</span></span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line">			String sql = <span class="string">&quot;insert into blog(phone_number, verification, blog_password) values(&#x27;123456&#x27;, &#x27;123456&#x27;, &#x27;123456&#x27;)&quot;</span>;</span><br><span class="line">			<span class="comment">// 执行 SQL 语句</span></span><br><span class="line">			<span class="keyword">int</span> num = statement.executeUpdate(sql);</span><br><span class="line">			<span class="keyword">if</span>(num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				System.out.println(<span class="string">&quot;用户连接成功!!!&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			<span class="comment">// 资源释放</span></span><br><span class="line">			<span class="keyword">if</span>(statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					statement.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				statement = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(connection != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					connection.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				connection = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-CRUD-操作之修改操作"><a href="#JDBC-的-CRUD-操作之修改操作" class="headerlink" title="JDBC 的 CRUD 操作之修改操作"></a>JDBC 的 CRUD 操作之修改操作</h4><h5 id="9-1-修改操作代码实现"><a href="#9-1-修改操作代码实现" class="headerlink" title="9.1 修改操作代码实现"></a>9.1 修改操作代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 注册驱动</span></span><br><span class="line">			Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://localhost:3306/ceshi&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;2caxcazdgn&quot;</span>);</span><br><span class="line">			<span class="comment">// 执行操作</span></span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句对象</span></span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line"><span class="comment">//			String sql = &quot;insert into blog(phone_number, verification, blog_password) values(&#x27;123456&#x27;, &#x27;123456&#x27;, &#x27;123456&#x27;)&quot;;</span></span><br><span class="line">			String sql = <span class="string">&quot;update blog set blog_password = 654321 where id = 6&quot;</span>;</span><br><span class="line">			<span class="comment">// 执行 SQL 语句</span></span><br><span class="line">			<span class="keyword">int</span> num = statement.executeUpdate(sql);</span><br><span class="line">			<span class="keyword">if</span>(num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				System.out.println(<span class="string">&quot;用户修改成功!!!&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			<span class="comment">// 资源释放</span></span><br><span class="line">			<span class="keyword">if</span>(statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					statement.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				statement = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(connection != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					connection.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				connection = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-CRUD-操作之删除操作"><a href="#JDBC-的-CRUD-操作之删除操作" class="headerlink" title="JDBC 的 CRUD 操作之删除操作"></a>JDBC 的 CRUD 操作之删除操作</h4><h5 id="10-1-删除操作代码实现"><a href="#10-1-删除操作代码实现" class="headerlink" title="10.1 删除操作代码实现"></a>10.1 删除操作代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 注册驱动</span></span><br><span class="line">			Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://localhost:3306/ceshi&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;2caxcazdgn&quot;</span>);</span><br><span class="line">			<span class="comment">// 执行操作</span></span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句对象</span></span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line"><span class="comment">//			String sql = &quot;insert into blog(phone_number, verification, blog_password) values(&#x27;123456&#x27;, &#x27;123456&#x27;, &#x27;123456&#x27;)&quot;;</span></span><br><span class="line"><span class="comment">//			String sql = &quot;update blog set blog_password = 654321 where id = 6&quot;;</span></span><br><span class="line">			String sql = <span class="string">&quot;delete from blog where id = 7&quot;</span>;</span><br><span class="line">			<span class="comment">// 执行 SQL 语句</span></span><br><span class="line">			<span class="keyword">int</span> num = statement.executeUpdate(sql);</span><br><span class="line">			<span class="keyword">if</span>(num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				System.out.println(<span class="string">&quot;用户删除成功!!!&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			<span class="comment">// 资源释放</span></span><br><span class="line">			<span class="keyword">if</span>(statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					statement.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				statement = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(connection != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					connection.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				connection = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-CRUD-操作之查询操作"><a href="#JDBC-的-CRUD-操作之查询操作" class="headerlink" title="JDBC 的 CRUD 操作之查询操作"></a>JDBC 的 CRUD 操作之查询操作</h4><h5 id="11-1-查询多条记录"><a href="#11-1-查询多条记录" class="headerlink" title="11.1 查询多条记录"></a>11.1 查询多条记录</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		ResultSet rSet = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 注册驱动</span></span><br><span class="line">			Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://localhost:3306/ceshi&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;2caxcazdgn&quot;</span>);</span><br><span class="line">			<span class="comment">// 执行操作</span></span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句对象</span></span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line"><span class="comment">//			String sql = &quot;insert into blog(phone_number, verification, blog_password) values(&#x27;123456&#x27;, &#x27;123456&#x27;, &#x27;123456&#x27;)&quot;;</span></span><br><span class="line"><span class="comment">//			String sql = &quot;update blog set blog_password = 654321 where id = 6&quot;;</span></span><br><span class="line"><span class="comment">//			String sql = &quot;delete from blog where id = 7&quot;;</span></span><br><span class="line">			String sql = <span class="string">&quot;select * from blog&quot;</span>;</span><br><span class="line">			<span class="comment">// 执行 SQL 语句</span></span><br><span class="line">			rSet = statement.executeQuery(sql);</span><br><span class="line">			<span class="keyword">while</span> (rSet.next()) &#123;</span><br><span class="line">				System.out.println(rSet.getInt(<span class="string">&quot;id&quot;</span>) + <span class="string">&quot; &quot;</span> + rSet.getString(<span class="string">&quot;blog_password&quot;</span>));</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			<span class="comment">// 资源释放</span></span><br><span class="line">      <span class="keyword">if</span>(rSet != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					rSet.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				rSet = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					statement.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				statement = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(connection != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					connection.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				connection = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h5 id="11-2-查询一条记录"><a href="#11-2-查询一条记录" class="headerlink" title="11.2 查询一条记录"></a>11.2 查询一条记录</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		ResultSet rSet = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 注册驱动</span></span><br><span class="line">			Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://localhost:3306/ceshi&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;2caxcazdgn&quot;</span>);</span><br><span class="line">			<span class="comment">// 执行操作</span></span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句对象</span></span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line"><span class="comment">//			String sql = &quot;insert into blog(phone_number, verification, blog_password) values(&#x27;123456&#x27;, &#x27;123456&#x27;, &#x27;123456&#x27;)&quot;;</span></span><br><span class="line"><span class="comment">//			String sql = &quot;update blog set blog_password = 654321 where id = 6&quot;;</span></span><br><span class="line"><span class="comment">//			String sql = &quot;delete from blog where id = 7&quot;;</span></span><br><span class="line">			String sql = <span class="string">&quot;select * from blog where id = 8&quot;</span>;</span><br><span class="line">			<span class="comment">// 执行 SQL 语句</span></span><br><span class="line">			rSet = statement.executeQuery(sql);</span><br><span class="line">			<span class="keyword">if</span>(rSet.next()) &#123;</span><br><span class="line">				System.out.println(rSet.getInt(<span class="string">&quot;id&quot;</span>) + <span class="string">&quot; &quot;</span> + rSet.getString(<span class="string">&quot;blog_password&quot;</span>));</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			<span class="comment">// 资源释放</span></span><br><span class="line">			<span class="keyword">if</span>(rSet != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					rSet.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				rSet = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					statement.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				statement = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(connection != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">try</span> &#123;</span><br><span class="line">					connection.close();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">					<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">					e2.printStackTrace();</span><br><span class="line">				&#125;</span><br><span class="line">				connection = <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的工具类的抽取"><a href="#JDBC-的工具类的抽取" class="headerlink" title="JDBC 的工具类的抽取"></a>JDBC 的工具类的抽取</h4><h5 id="12-1-抽取一个-JDBC-的工具类"><a href="#12-1-抽取一个-JDBC-的工具类" class="headerlink" title="12.1 抽取一个 JDBC 的工具类"></a>12.1 抽取一个 JDBC 的工具类</h5><p>因为传统 JDBC 的开发, 注册驱动、获得连接、释放资源这些代码都是重复的. 所以可以将重复的代码提取到一个类中来完成.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.utils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCUtils</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String driverClassName;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String url;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String username;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String password;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> &#123;</span><br><span class="line">		driverClassName = <span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>;</span><br><span class="line">		url = <span class="string">&quot;jdbc:mysql:///ceshi&quot;</span>;</span><br><span class="line">		username = <span class="string">&quot;root&quot;</span>;</span><br><span class="line">		password = <span class="string">&quot;2caxcazdgn&quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 注册驱动</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">loadDriver</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			Class.forName(driverClassName);</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 获得连接</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Connection <span class="title">getConnection</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			loadDriver();</span><br><span class="line">			connection = DriverManager.getConnection(url, username, password);</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> connection;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 释放资源</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">release</span><span class="params">(Statement statement, Connection connection)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// 资源释放</span></span><br><span class="line">		<span class="keyword">if</span>(statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">try</span> &#123;</span><br><span class="line">				statement.close();</span><br><span class="line">			&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">				<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">				e2.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			statement = <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(connection != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">try</span> &#123;</span><br><span class="line">				connection.close();</span><br><span class="line">			&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">				<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">				e2.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			connection = <span class="keyword">null</span>;</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">release</span><span class="params">(ResultSet rSet, Statement statement, Connection connection)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// 资源释放</span></span><br><span class="line">		<span class="keyword">if</span>(rSet != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">try</span> &#123;</span><br><span class="line">				rSet.close();</span><br><span class="line">			&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">				<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">				e2.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			rSet = <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">try</span> &#123;</span><br><span class="line">				statement.close();</span><br><span class="line">			&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">				<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">				e2.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			statement = <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(connection != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">try</span> &#123;</span><br><span class="line">				connection.close();</span><br><span class="line">			&#125; <span class="keyword">catch</span> (Exception e2) &#123;</span><br><span class="line">				<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">				e2.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			connection = <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h5 id="12-2-测试工具类"><a href="#12-2-测试工具类" class="headerlink" title="12.2 测试工具类"></a>12.2 测试工具类</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo3;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo3</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		ResultSet rSet = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句的对象</span></span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line">			String sql = <span class="string">&quot;select * from blog&quot;</span>;</span><br><span class="line">			<span class="comment">// 执行查询</span></span><br><span class="line">			rSet = statement.executeQuery(sql);</span><br><span class="line">			<span class="comment">// 遍历结果集</span></span><br><span class="line">			<span class="keyword">while</span>(rSet.next()) &#123;</span><br><span class="line">				System.out.println(rSet.getInt(<span class="string">&quot;id&quot;</span>) + <span class="string">&quot; &quot;</span> + rSet.getString(<span class="string">&quot;blog_password&quot;</span>));</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(rSet, statement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的配置信息提取到配置文件"><a href="#JDBC-的配置信息提取到配置文件" class="headerlink" title="JDBC 的配置信息提取到配置文件"></a>JDBC 的配置信息提取到配置文件</h4><h5 id="13-1-配置文件"><a href="#13-1-配置文件" class="headerlink" title="13.1 配置文件"></a>13.1 配置文件</h5><ul>
<li>属性文件<ul>
<li>格式: 扩展名是 .properties</li>
<li>内容: key=value</li>
</ul>
</li>
<li>XML 文件</li>
</ul>
<h5 id="13-2-提取信息到配置文件"><a href="#13-2-提取信息到配置文件" class="headerlink" title="13.2 提取信息到配置文件"></a>13.2 提取信息到配置文件</h5><ul>
<li>定义一个配置文件 </li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">driverClassName = com.mysql.cj.jdbc.Driver</span><br><span class="line">url = jdbc:mysql:<span class="comment">//localhost:3306/ceshi</span></span><br><span class="line">username = root</span><br><span class="line">password = 2caxcazdgn</span><br></pre></td></tr></table></figure>

<h5 id="13-3-在工具类中解析属性文件"><a href="#13-3-在工具类中解析属性文件" class="headerlink" title="13.3 在工具类中解析属性文件"></a>13.3 在工具类中解析属性文件</h5><ul>
<li>获取到具体内容为常量赋值</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> &#123;</span><br><span class="line">		<span class="comment">// 获取属性文件中的内容</span></span><br><span class="line">		Properties properties = <span class="keyword">new</span> Properties();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			properties.load(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;src/db.properties&quot;</span>));</span><br><span class="line">		&#125; <span class="keyword">catch</span> (FileNotFoundException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		driverClassName = properties.getProperty(<span class="string">&quot;driverClassName&quot;</span>);</span><br><span class="line">		url = properties.getProperty(<span class="string">&quot;url&quot;</span>);</span><br><span class="line">		username = properties.getProperty(<span class="string">&quot;username&quot;</span>);</span><br><span class="line">		password = properties.getProperty(<span class="string">&quot;password&quot;</span>);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-SQL-注入漏洞"><a href="#JDBC-的-SQL-注入漏洞" class="headerlink" title="JDBC 的 SQL 注入漏洞"></a>JDBC 的 SQL 注入漏洞</h4><h5 id="14-1-什么是-SQL-注入漏洞"><a href="#14-1-什么是-SQL-注入漏洞" class="headerlink" title="14.1 什么是 SQL 注入漏洞"></a>14.1 什么是 SQL 注入漏洞</h5><p>在早期的互联网上 SQL 注入漏洞普遍存在, 有一个网站, 用户需要注入, 用户注册以后根据用户名和密码完成登陆, 假设现在已经用户名被其他人知道了, 但是其他人不知道你的密码, 也可以登录到网站上进行相应的操作</p>
<h5 id="14-2-演示-SQL-注入漏洞"><a href="#14-2-演示-SQL-注入漏洞" class="headerlink" title="14.2 演示 SQL 注入漏洞"></a>14.2 演示 SQL 注入漏洞</h5><ul>
<li>基本登陆功能实现</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo4;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserDao</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">login</span><span class="params">(String username, String password)</span> </span>&#123;</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		ResultSet rSet = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 定义一个变量</span></span><br><span class="line">		<span class="keyword">boolean</span> flag = <span class="keyword">false</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			<span class="comment">// 完成登陆功能</span></span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句的对象</span></span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line">			String sql = <span class="string">&quot;select * from blog where phone_number = &#x27;&quot;</span> + username + <span class="string">&quot;&#x27; and blog_password = &#x27;&quot;</span> + password + <span class="string">&quot;&#x27;&quot;</span>;</span><br><span class="line">			<span class="comment">// 执行 SQL 语句</span></span><br><span class="line">			rSet = statement.executeQuery(sql);</span><br><span class="line">			<span class="keyword">if</span>(rSet.next()) &#123;</span><br><span class="line">				<span class="comment">// 说明根据用户名和密码可以查询到这条记录</span></span><br><span class="line">				flag = <span class="keyword">true</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(rSet, statement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>演示 SQL 注入漏洞<ul>
<li>输入用户名<ul>
<li>aaa’or’1=1 密码随意</li>
<li>aaa’–          密码随意</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo4;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo4</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		UserDao userDao = <span class="keyword">new</span> UserDao();</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		boolean flag = userDao.login(&quot;15830233129&#x27; or &#x27;1=1&quot;, &quot;123sadasdas456&quot;);</span></span><br><span class="line">		<span class="keyword">boolean</span> flag = userDao.login(<span class="string">&quot;15830233129&#x27; -- &quot;</span>, <span class="string">&quot;123sadasdas456&quot;</span>);</span><br><span class="line"></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(flag) &#123;</span><br><span class="line">			System.out.println(<span class="string">&quot;登录成功&quot;</span>);</span><br><span class="line">		&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">			System.out.println(<span class="string">&quot;登录失败&quot;</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-SQL-注入漏洞的分析和解决"><a href="#JDBC-的-SQL-注入漏洞的分析和解决" class="headerlink" title="JDBC 的 SQL 注入漏洞的分析和解决"></a>JDBC 的 SQL 注入漏洞的分析和解决</h4><h5 id="15-1-SQL-注入漏洞分析"><a href="#15-1-SQL-注入漏洞分析" class="headerlink" title="15.1 SQL 注入漏洞分析"></a>15.1 SQL 注入漏洞分析</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">演示 SQL 注入漏洞</span><br><span class="line"></span><br><span class="line">- 输入用户名</span><br><span class="line">  - aaa&#x27;or&#x27;1=1 密码随意</span><br><span class="line">  - aaa&#x27;--     密码随意</span><br><span class="line">  </span><br><span class="line">原因: </span><br><span class="line">	- 在变量中存在SQL的关键字</span><br><span class="line">  	- or</span><br><span class="line">  	- --</span><br></pre></td></tr></table></figure>

<h5 id="15-2-SQL-注入漏洞解决"><a href="#15-2-SQL-注入漏洞解决" class="headerlink" title="15.2 SQL 注入漏洞解决"></a>15.2 SQL 注入漏洞解决</h5><p>需要采用 PreparedStatement 对象解决 SQL 注入漏洞. 这个对象将 SQL 预先进行编译, 使用 ? 作为占位符. ? 代表的内容是 SQL 所固定的, </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo4;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserDao</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">login</span><span class="params">(String username, String password)</span> </span>&#123;</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement statement = <span class="keyword">null</span>;</span><br><span class="line">		ResultSet rSet = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 定义一个变量</span></span><br><span class="line">		<span class="keyword">boolean</span> flag = <span class="keyword">false</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			<span class="comment">// 完成登陆功能</span></span><br><span class="line">			<span class="comment">// 创建执行 SQL 语句的对象</span></span><br><span class="line">			</span><br><span class="line">			<span class="comment">// 编写 SQL 语句</span></span><br><span class="line">			String sql = <span class="string">&quot;select * from blog where phone_number = ? and blog_password = ?&quot;</span>;</span><br><span class="line">			statement = connection.prepareStatement(sql);</span><br><span class="line">			<span class="comment">// 设置参数</span></span><br><span class="line">			statement.setString(<span class="number">1</span>, username);</span><br><span class="line">			statement.setString(<span class="number">2</span>, password);</span><br><span class="line">			System.out.println(sql);</span><br><span class="line">			<span class="comment">// 执行 SQL 语句</span></span><br><span class="line">			rSet = statement.executeQuery();</span><br><span class="line">			<span class="keyword">if</span>(rSet.next()) &#123;</span><br><span class="line">				<span class="comment">// 说明根据用户名和密码可以查询到这条记录</span></span><br><span class="line">				flag = <span class="keyword">true</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(rSet, statement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">return</span> flag;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-CRUD-操作之-PreparedStatement-的保存操作"><a href="#JDBC-的-CRUD-操作之-PreparedStatement-的保存操作" class="headerlink" title="JDBC 的 CRUD 操作之 PreparedStatement 的保存操作"></a>JDBC 的 CRUD 操作之 PreparedStatement 的保存操作</h4><h5 id="16-1-保存操作代码实现"><a href="#16-1-保存操作代码实现" class="headerlink" title="16.1 保存操作代码实现"></a>16.1 保存操作代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo5;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDmeo5</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement pStatement = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="comment">// 获得连接</span></span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			</span><br><span class="line">			<span class="comment">// SQL 语句</span></span><br><span class="line">			String sql = <span class="string">&quot;insert into blog values(null, ?, ?, ?)&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			<span class="comment">// 预编译 SQL</span></span><br><span class="line">			pStatement = connection.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			<span class="comment">// 设置参数</span></span><br><span class="line">			pStatement.setString(<span class="number">1</span>, <span class="string">&quot;12367876&quot;</span>);</span><br><span class="line">			pStatement.setString(<span class="number">2</span>, <span class="string">&quot;wuyanzhengma &quot;</span>);</span><br><span class="line">			pStatement.setString(<span class="number">3</span>, <span class="string">&quot;0987654321&quot;</span>);</span><br><span class="line"></span><br><span class="line">			<span class="comment">// 执行 SQL </span></span><br><span class="line">			<span class="keyword">int</span> num = pStatement.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">if</span>(num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				System.out.println(<span class="string">&quot;保存成功&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(pStatement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-CRUD-操作之-PreparedStatement-的修改操作"><a href="#JDBC-的-CRUD-操作之-PreparedStatement-的修改操作" class="headerlink" title="JDBC 的 CRUD 操作之 PreparedStatement 的修改操作"></a>JDBC 的 CRUD 操作之 PreparedStatement 的修改操作</h4><h5 id="17-1-修改操作代码实现"><a href="#17-1-修改操作代码实现" class="headerlink" title="17.1 修改操作代码实现"></a>17.1 修改操作代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo6;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo6</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement pStatement = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			</span><br><span class="line">			String sql = <span class="string">&quot;update blog set password = ? where id = 3&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			pStatement = connection.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			pStatement.setString(<span class="number">1</span>, <span class="string">&quot;000000&quot;</span>);</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">int</span> num = pStatement.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">if</span>(num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				System.out.println(<span class="string">&quot;修改成功&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(pStatement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-CRUD-操作之-PreparedStatement-的删除操作"><a href="#JDBC-的-CRUD-操作之-PreparedStatement-的删除操作" class="headerlink" title="JDBC 的 CRUD 操作之 PreparedStatement 的删除操作"></a>JDBC 的 CRUD 操作之 PreparedStatement 的删除操作</h4><h5 id="18-1-删除操作的代码实现"><a href="#18-1-删除操作的代码实现" class="headerlink" title="18.1 删除操作的代码实现"></a>18.1 删除操作的代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo7;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo7</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement pStatement = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			</span><br><span class="line">			String sql = <span class="string">&quot;delete from blog where id = ?&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			pStatement = connection.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			pStatement.setInt(<span class="number">1</span>, <span class="number">15</span>);</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">int</span> num = pStatement.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">if</span>(num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				System.out.println(<span class="string">&quot;删除成功&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(pStatement, connection);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的-CRUD-操作之-PreparedStatement-的查询操作"><a href="#JDBC-的-CRUD-操作之-PreparedStatement-的查询操作" class="headerlink" title="JDBC 的 CRUD 操作之 PreparedStatement 的查询操作"></a>JDBC 的 CRUD 操作之 PreparedStatement 的查询操作</h4><h5 id="19-1-查询操作代码实现"><a href="#19-1-查询操作代码实现" class="headerlink" title="19.1 查询操作代码实现"></a>19.1 查询操作代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo8;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"><span class="keyword">import</span> java.sql.ResultSet;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo8</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement pStatement = <span class="keyword">null</span>;</span><br><span class="line">		ResultSet rSet = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			</span><br><span class="line">			String sql = <span class="string">&quot;select * from blog&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			pStatement = connection.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			rSet = pStatement.executeQuery();</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">while</span>(rSet.next()) &#123;</span><br><span class="line">				System.out.println(rSet.getInt(<span class="string">&quot;id&quot;</span>) + <span class="string">&quot; &quot;</span> + rSet.getString(<span class="string">&quot;phone&quot;</span>));</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line"> 		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(rSet, pStatement, connection);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的批处理"><a href="#JDBC-的批处理" class="headerlink" title="JDBC 的批处理"></a>JDBC 的批处理</h4><h5 id="20-1-什么是批处理"><a href="#20-1-什么是批处理" class="headerlink" title="20.1 什么是批处理"></a>20.1 什么是批处理</h5><p>之前进行 JDBC 的操作的时候, 都是一条 SQL 语句执行, 现在如果使用批处理, 可以将一批 SQL 一起执行</p>
<h5 id="20-2-批处理基本使用"><a href="#20-2-批处理基本使用" class="headerlink" title="20.2 批处理基本使用"></a>20.2 批处理基本使用</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo9;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.Statement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo9</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		<span class="comment">// 批处理的基本操作</span></span><br><span class="line">		</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		Statement statement = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			</span><br><span class="line">			statement = connection.createStatement();</span><br><span class="line">			</span><br><span class="line">			String sql1 = <span class="string">&quot;create database test1&quot;</span>;</span><br><span class="line">			String sql2 = <span class="string">&quot;use test1&quot;</span>;</span><br><span class="line">			String sql3 = <span class="string">&quot;create table user(id int primary key auto_increment, name varchar(20))&quot;</span>;</span><br><span class="line">			String sql4 = <span class="string">&quot;insert into user values(null, &#x27;aaa&#x27;)&quot;</span>;</span><br><span class="line">			String sql5 = <span class="string">&quot;insert into user values(null, &#x27;bbb&#x27;)&quot;</span>;</span><br><span class="line">			String sql6 = <span class="string">&quot;insert into user values(null, &#x27;ccc&#x27;)&quot;</span>;</span><br><span class="line">			String sql7 = <span class="string">&quot;update user set name = &#x27;mmm&#x27; where id = 2&quot;</span>;</span><br><span class="line">			String sql8 = <span class="string">&quot;delete from user where id = 1&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			<span class="comment">// 添加到批处理</span></span><br><span class="line">			statement.addBatch(sql1);</span><br><span class="line">			statement.addBatch(sql2);</span><br><span class="line">			statement.addBatch(sql3);</span><br><span class="line">			statement.addBatch(sql4);</span><br><span class="line">			statement.addBatch(sql5);</span><br><span class="line">			statement.addBatch(sql6);</span><br><span class="line">			statement.addBatch(sql7);</span><br><span class="line">			statement.addBatch(sql8);</span><br><span class="line">			</span><br><span class="line">			<span class="comment">// 执行批处理</span></span><br><span class="line">			statement.executeBatch();</span><br><span class="line"></span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(statement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h5 id="20-3-批量插入-使用-PreparedStatement"><a href="#20-3-批量插入-使用-PreparedStatement" class="headerlink" title="20.3 批量插入 (使用 PreparedStatement)"></a>20.3 批量插入 (使用 PreparedStatement)</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.jdbc.demo9;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JDBCDemo9_PreparedStatement</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		<span class="comment">// 默认情况下, MySQL 没有开启批处理, 需要在 URL 后拼接一个参数</span></span><br><span class="line">		<span class="comment">// 记录开始时间</span></span><br><span class="line">		<span class="keyword">long</span> begin = System.currentTimeMillis();</span><br><span class="line"></span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement pStatement = <span class="keyword">null</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			</span><br><span class="line">			String sqlString = <span class="string">&quot;insert into user values(null, ?)&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			pStatement = connection.prepareStatement(sqlString);</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=<span class="number">10000</span>; i++) &#123;</span><br><span class="line">				pStatement.setString(<span class="number">1</span>, <span class="string">&quot;name&quot;</span> + i);</span><br><span class="line">				</span><br><span class="line">				<span class="comment">// 添加到批处理</span></span><br><span class="line">				pStatement.addBatch();</span><br><span class="line">				</span><br><span class="line">				<span class="comment">// 注意问题</span></span><br><span class="line">				<span class="comment">// 执行批处理</span></span><br><span class="line">				<span class="keyword">if</span>(i % <span class="number">1000</span> == <span class="number">0</span>) &#123;</span><br><span class="line">					<span class="comment">// 执行批处理</span></span><br><span class="line">					pStatement.executeBatch();</span><br><span class="line">					<span class="comment">// 清除批处理</span></span><br><span class="line">					pStatement.clearBatch();</span><br><span class="line">					</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;		</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(pStatement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 记录结束时间</span></span><br><span class="line">		<span class="keyword">long</span> end = System.currentTimeMillis();</span><br><span class="line">		</span><br><span class="line">		System.out.println((end - begin));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的事物环境准备"><a href="#JDBC-的事物环境准备" class="headerlink" title="JDBC 的事物环境准备"></a>JDBC 的事物环境准备</h4><h5 id="21-1-事务的概念"><a href="#21-1-事务的概念" class="headerlink" title="21.1 事务的概念"></a>21.1 事务的概念</h5><p>事务指的是逻辑上的一组操作, 组成这组操作的各个逻辑单元要么全部成功, 要么全部失败</p>
<h5 id="21-2-事物环境准备"><a href="#21-2-事物环境准备" class="headerlink" title="21.2 事物环境准备"></a>21.2 事物环境准备</h5><ul>
<li>创建数据库和表单</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">create database lummer;</span><br><span class="line">use lummer;</span><br><span class="line">create table account (</span><br><span class="line"> 	id int primary key auto_increment,</span><br><span class="line"> 	name varchar(20),</span><br><span class="line"> 	money double</span><br><span class="line">);</span><br><span class="line">insert into account values(null, &quot;aaa&quot;, 10000);</span><br><span class="line">insert into account values(null, &quot;bbb&quot;, 10000);</span><br><span class="line">insert into account values(null, &quot;ccc&quot;, 10000);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="JDBC-的事务管理"><a href="#JDBC-的事务管理" class="headerlink" title="JDBC 的事务管理"></a>JDBC 的事务管理</h4><h5 id="22-1-转账案例代码实现"><a href="#22-1-转账案例代码实现" class="headerlink" title="22.1 转账案例代码实现"></a>22.1 转账案例代码实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> jdbc_transaction;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransactionDemo1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">demo1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement pStatement = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			</span><br><span class="line">			String sql = <span class="string">&quot;update blog set password = password + ? where id = ?&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			pStatement = connection.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			pStatement.setString(<span class="number">1</span>, <span class="string">&quot;-10000&quot;</span>);</span><br><span class="line">			pStatement.setInt(<span class="number">2</span>, <span class="number">1</span>);</span><br><span class="line">			</span><br><span class="line">			pStatement.setString(<span class="number">1</span>, <span class="string">&quot;10000&quot;</span>);</span><br><span class="line">			pStatement.setInt(<span class="number">2</span>, <span class="number">2</span>);</span><br><span class="line">			</span><br><span class="line">			pStatement.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			System.out.println(<span class="string">&quot;转账成功&quot;</span>);</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(pStatement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 在转账中没有添加事务的管理, 导致 id=1 的帐号的钱被转丢了, 但是 id=2 的帐号的钱没有任何变化. 需要给转账的功能添加事务的管理.</span></span><br></pre></td></tr></table></figure>

<h5 id="22-2-在转账中添加事务管理"><a href="#22-2-在转账中添加事务管理" class="headerlink" title="22.2 在转账中添加事务管理"></a>22.2 在转账中添加事务管理</h5><ul>
<li><p>事务管理的 API</p>
<ul>
<li>setAutoCommit()</li>
<li>commit()</li>
<li>rollback()</li>
</ul>
</li>
<li><p>在转账中添加事务管理</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> jdbc_transaction;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.PreparedStatement;</span><br><span class="line"><span class="keyword">import</span> java.sql.SQLException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima.jdbc.utils.JDBCUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransactionDemo1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">demo1</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">		Connection connection = <span class="keyword">null</span>;</span><br><span class="line">		PreparedStatement pStatement = <span class="keyword">null</span>;</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			connection = JDBCUtils.getConnection();</span><br><span class="line">			connection.setAutoCommit(<span class="keyword">false</span>);</span><br><span class="line">			</span><br><span class="line">			String sql = <span class="string">&quot;update blog set password = password + ? where id = ?&quot;</span>;</span><br><span class="line">			</span><br><span class="line">			pStatement = connection.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			pStatement.setString(<span class="number">1</span>, <span class="string">&quot;-10000&quot;</span>);</span><br><span class="line">			pStatement.setInt(<span class="number">2</span>, <span class="number">1</span>);</span><br><span class="line">			</span><br><span class="line">			pStatement.setString(<span class="number">1</span>, <span class="string">&quot;10000&quot;</span>);</span><br><span class="line">			pStatement.setInt(<span class="number">2</span>, <span class="number">2</span>);</span><br><span class="line">			</span><br><span class="line">			pStatement.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			System.out.println(<span class="string">&quot;转账成功&quot;</span>);</span><br><span class="line">			connection.commit();</span><br><span class="line">		&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">			connection.rollback();</span><br><span class="line">			<span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			JDBCUtils.release(pStatement, connection);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>




      
    </div>
    <footer class="article-footer">
      <a data-url="https://lummer.gitee.io/hexo/2021/01/26/JDBC/" data-id="cktshq5ab000adu9leo6yff2l" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/02/24/Utils/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Utils
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/01/16/MySQL%20%E9%87%8D%E7%BD%AE%E5%AF%86%E7%A0%81/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">MySQL 重置密码</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/09/">September 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/03/">March 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/09/15/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/hexo/2021/08/20/Mac%20%E5%85%A8%E5%B1%8F%E6%97%B6%E8%87%AA%E5%8A%A8%E9%9A%90%E8%97%8F%E7%A8%8B%E5%BA%8F%E5%9D%9E/">Mac 全屏时自动隐藏程序坞</a>
          </li>
        
          <li>
            <a href="/hexo/2021/07/29/Mac%E7%B3%BB%E7%BB%9F%E4%B8%AD%E7%9A%84.DS_Store/">Mac系统中的.DS_Store</a>
          </li>
        
          <li>
            <a href="/hexo/2021/07/21/Markdown/">Markdown</a>
          </li>
        
          <li>
            <a href="/hexo/2021/07/11/Github%E5%9B%BD%E5%86%85%E9%95%9C%E5%83%8F%E7%BD%91%E7%AB%99%EF%BC%8C%E8%A7%A3%E5%86%B3Github%E8%AE%BF%E9%97%AE%E7%9A%84%E7%A5%9E%E5%99%A8/">Github国内镜像网站，解决Github访问的神器</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.css">

  
<script src="/hexo/fancybox/jquery.fancybox.pack.js"></script>




<script src="/hexo/js/script.js"></script>




  </div>
</body>
</html>